/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ViewMain.java
 *
 * Created on 19/08/2010, 23:23:52
 */
package view;

import controller.FacadeHost;
import model.serialization.Serializer;
import interfaces.PersistenceModuleInterface;
import java.awt.Component;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author evaristowb
 */
public class ViewMain extends javax.swing.JFrame {

    /** Creates new form ViewMain */
    public ViewMain() {
        initComponents();
        FacadeHost.setViewMain(this);
       
        /*
        try {
            FacadeHost.getViewMain().setIconImage(new ImageIcon("./images/icon.jpg").getImage());
        } catch (Exception e) {
        }
        */
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPAbas1 = new view.jpanel.JPanelTabbed();
        jMenuBar = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItemSave = new javax.swing.JMenuItem();
        jMenuItemOpen = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        jMenuItemExit = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItemAbout = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("YADMT - Yet Another Data Mining Tool");

        jMenu1.setText("Arquivo");

        jMenuItemSave.setText("Salvar");
        jMenuItemSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemSaveActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItemSave);

        jMenuItemOpen.setText("Abrir");
        jMenuItemOpen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemOpenActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItemOpen);
        jMenu1.add(jSeparator1);

        jMenuItemExit.setText("Sair");
        jMenuItemExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemExitActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItemExit);

        jMenuBar.add(jMenu1);

        jMenu2.setText("Ajuda");

        jMenuItemAbout.setText("Sobre");
        jMenuItemAbout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemAboutActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItemAbout);

        jMenuBar.add(jMenu2);

        setJMenuBar(jMenuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPAbas1, javax.swing.GroupLayout.DEFAULT_SIZE, 755, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPAbas1, javax.swing.GroupLayout.DEFAULT_SIZE, 528, Short.MAX_VALUE)
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-771)/2, (screenSize.height-588)/2, 771, 588);
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItemSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemSaveActionPerformed
        Component comp = jPAbas1.getSelectedConponent();
        if (comp instanceof PersistenceModuleInterface) {
            saveObject(((PersistenceModuleInterface) comp).getSave());
        }
    }//GEN-LAST:event_jMenuItemSaveActionPerformed

    private void jMenuItemOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemOpenActionPerformed
        JFileChooser fileChooser = new JFileChooser();
        if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
            loadFile(fileChooser.getSelectedFile().getPath());
        }
    }//GEN-LAST:event_jMenuItemOpenActionPerformed

    private void jMenuItemExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemExitActionPerformed
        System.exit(0);
    }//GEN-LAST:event_jMenuItemExitActionPerformed

    private void jMenuItemAboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemAboutActionPerformed
        JOptionPane.showMessageDialog(this,"YADMT 2010-2012","Sobre",JOptionPane.INFORMATION_MESSAGE);
    }//GEN-LAST:event_jMenuItemAboutActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(ViewMain.class.getName()).log(Level.SEVERE, null, ex);
                } catch (InstantiationException ex) {
                    Logger.getLogger(ViewMain.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(ViewMain.class.getName()).log(Level.SEVERE, null, ex);
                } catch (UnsupportedLookAndFeelException ex) {
                    Logger.getLogger(ViewMain.class.getName()).log(Level.SEVERE, null, ex);
                }
                
                new ViewMain().setVisible(true);              
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar;
    private javax.swing.JMenuItem jMenuItemAbout;
    private javax.swing.JMenuItem jMenuItemExit;
    private javax.swing.JMenuItem jMenuItemOpen;
    private javax.swing.JMenuItem jMenuItemSave;
    private view.jpanel.JPanelTabbed jPAbas1;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    // End of variables declaration//GEN-END:variables

    private void saveObject(Object o) {
        JFileChooser fileChooser = new JFileChooser();
        if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
        Serializer.Writer(o, fileChooser.getSelectedFile());
            
//            FileOutputStream fo = null;
//            {
//                ObjectOutputStream oo = null;
//                try {
////                    String nome = fileChooser.getSelectedFile().getPath();
////                    File fileName = new File(nome);
//                    fo = new FileOutputStream(fileChooser.getSelectedFile());
//                    oo = new ObjectOutputStream(fo);
//                    oo.writeObject(o);
//                    oo.flush();
//                    oo.close();
//                    fo.flush();
//                    //System.out.println("salvou");
//                } catch (IOException ex) {
//                    Logger.getLogger(ViewMain.class.getName()).log(Level.SEVERE, null, ex);
//                } finally {
//                    try {
//                        fo.close();
//                    } catch (IOException ex) {
//                        Logger.getLogger(ViewMain.class.getName()).log(Level.SEVERE, null, ex);
//                    }
//                    try {
//                        oo.close();
//                    } catch (IOException ex) {
//                        Logger.getLogger(ViewMain.class.getName()).log(Level.SEVERE, null, ex);
//                    }
//                }
//            }

        }
    }

    /**
     * load file
     * @param path
     */
    private void loadFile(String path) {
        Object o = Serializer.Reader(path);
        if(o==null){
            JOptionPane.showMessageDialog(this, "Módulo não suportado");
        }else{
           FacadeHost.getHost().addModule(o);
        }        

//        Component comp = jPAbas1.getSelectedConponent();
//        if (comp instanceof PersistenceModuleInterface) {
//            saveObject(((PersistenceModuleInterface) comp).getSave());
//        }

//        FileInputStream fi = null;
//        {
//            ObjectInputStream oi = null;
//            try {
//                fi = new FileInputStream(path);
//                oi = new ObjectInputStream(fi);
//
//                Object a = (Object)oi.readObject();
//                //c = (Cat) oi.readObject();
//                oi.close();
//            } catch (IOException ex) {
//                Logger.getLogger(ViewMain.class.getName()).log(Level.SEVERE, null, ex);
//            } catch (ClassNotFoundException ex) {
//                Logger.getLogger(ViewMain.class.getName()).log(Level.SEVERE, null, ex);
//            }  finally {
//                try {
//                    fi.close();
//                } catch (IOException ex) {
//                    Logger.getLogger(ViewMain.class.getName()).log(Level.SEVERE, null, ex);
//                }
//                try {
//                    oi.close();
//                } catch (IOException ex) {
//                    Logger.getLogger(ViewMain.class.getName()).log(Level.SEVERE, null, ex);
//                }
//            }
//        }
    }
}
